using System;
using System.Collections.Generic;
using System.Text;

namespace PSE.Hosting.Management.Console
{
    internal class CommandFactory
    {
        // Available commands
        private const string CreatePackageCommand = "/createpackage";
        private const string RemovePackageCommand = "/removepackage";
        private const string StartPackageCommand = "/startpackage";
        private const string StopPackageCommand = "/stoppackage";
        private const string AddAssemblyCommand = "/add";
        private const string RemoveAssemblyCommand = "/remove";
        private const string VerifyAssemblyCommand = "/verify";
        private const string StartAssemblyCommand = "/start";
        private const string StopAssemblyCommand = "/stop";
        private const string StopAllCommand = "/stopall";
        private const string HelpCommand = "/help";
        private const string STOPSERVICE = "STOPSERVICE";
        private const string STARTSERVICE = "STARTSERVICE";

        // Short name for the available commands.
        private const string CreatePackageShortCommand = "/cp";
        private const string RemovePackageShortCommand = "/rp";
        private const string StartPackageShortCommand = "/stp";
        private const string StopPackageShortCommand = "/spp";
        private const string AddAssemblyShortCommand = "/a";
        private const string RemoveAssemblyShortCommand = "/r";
        private const string StartAssemblyShortCommand = "/st";
        private const string StopAssemblyShortCommand = "/sp";
        private const string HelpShortCommand = "/?";

        private static Dictionary<string, ICommand> _instances = new Dictionary<string,ICommand>();

        internal static ICommand Instance(string commandName)
        {
            if (_instances.ContainsKey(commandName))
            {
                return _instances[commandName];
            }

            ICommand command = null;

            switch (commandName)
            {
                case CreatePackageCommand:
                case CreatePackageShortCommand:
                    command = new CreatePackageCommand();
                    _instances.Add(commandName, command);
                    return command;

                case RemovePackageCommand:
                case RemovePackageShortCommand:
                    command = new RemovePackageCommand();
                    _instances.Add(commandName, command);
                    return command;
    
                case StartPackageCommand:
                case StartPackageShortCommand:
                    command = new StartPackageCommand();
                    _instances.Add(commandName, command);
                    return command;

                //case StopPackageCommand:
                //case StopPackageShortCommand:
                //    command = new StopPackageCommand();
                //    _instances.Add(commandName, command);
                //    return command;

                case AddAssemblyCommand:
                case AddAssemblyShortCommand:
                    command = new AddAssemblyCommand();
                    _instances.Add(commandName, command);
                    return command;

                case StartAssemblyCommand:
                case StartAssemblyShortCommand:
                    command = new StartAssemblyCommand();
                    _instances.Add(commandName, command);
                    return command;

                case StopAssemblyCommand:
                case StopAssemblyShortCommand:
                    command = new StopAssemblyCommand();
                    _instances.Add(commandName, command);
                    return command;

                case HelpCommand:
                case HelpShortCommand:
                    command = new HelpCommand();
                    _instances.Add(commandName, command);
                    return command;

                default:
                    Messages.CommandSyntaxError(commandName);
                    return null;
            }

        }
    }
}
